package 爬虫;

import com.alibaba.fastjson.JSON;
import com.util.httpUtil.HttpClientResult;
import com.util.httpUtil.HttpClientUtils;
import com.util.StringUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CHL
 * @ClassName: PaChongUtils
 * @Description: 车辆
 * @date 2023/4/20 15:14
 * @Version 1.0
 */
public class PaChongUtils {


    /**
     * @return response response
     * @Author chl
     * @Description 懂车帝车辆信息
     * @Date 15:16 2023/4/20
     **/
    public static List<DongCheDiCarDto> getQiCheZhiJia(String city, String modelType) {
        if (StringUtil.isEmpty(city)) {
            city = "a0_0msdgscncgpi1ltocspex"; // 默认全车型
        }
        if (StringUtil.isEmpty(modelType)) {
            modelType = "benchiglb"; // 默认GLB
        }

        // 带glb条件查询
        String urlGlb = "https://car.autohome.com.cn/2sc/%s/benchi/%s/";

        urlGlb = String.format(urlGlb, city, modelType);
        HttpClientResult httpClientResult = null;
        try {
            httpClientResult = HttpClientUtils.doGet(urlGlb);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String content = httpClientResult.getContent();
        Document parse = Jsoup.parse(content);
        // 通过工具获取html标签,  通过页面解析数据在这个层级下：div div div div div div ul li
        // 通过li[onmouseout] 来锁定需要的车辆信息
        Elements elements = parse.select("div div div div div div ul li[onmouseout]");

        // 返回类
        List<DongCheDiCarDto> voList = new ArrayList();
        for (int i = 0; i < elements.size(); i++) {
            DongCheDiCarDto carDto = new DongCheDiCarDto();

            Element element = elements.get(i);

            // 每一个li标签下有多个div标签，通过 div[对应标签下属性值来区别]
            Elements pic = element.select("div[class=pic]");
            if (pic.size() > 0) {
                Elements select = pic.select("a");
                if (select.size() > 0) {
                    // 车辆详情访问请求地址
                    String href = select.attr("href");
                }
                Elements eimg = pic.select("img");
                if (eimg.size() > 0) {
                    // 图片url
                    String src2 = eimg.attr("src2");
                    carDto.setImage(src2);
                    // 车辆标题
                    String title = eimg.attr("title");
                    carDto.setTitle(title);
                    String[] split = title.split(" ");

                    String carName = "";
                    for (int j = 0; j < split.length; j++) {
                        if (j == 0) {
                            // 车型
                            carDto.setSeries_name(split[0]);
                        } else {
                            // 车款
                            carName += split[j];
                        }
                    }
                    carDto.setCar_name(carName);
                }
            }

            Elements datail = element.select("div[class=detail]");
            if (datail.size() > 0) {
                // 车辆价格
                Elements detail_r = datail.select("div div span");
                if (detail_r.size() > 0) {
                    String price = detail_r.html();
                    carDto.setSh_price(price);
                }

                // 公里数、年限
                Elements p = datail.select("div div p");
                if (p.size() > 0) {
                    String html = p.html();
                    String replace = html.replace("\n", " ");
                    String[] split1 = replace.split(" ");
                    String[] split = split1[0].split("：");
                    String gongli = split[1];
                    carDto.setSub_title(gongli);

                    carDto.setCar_year(split1[2]);
                }
            }

            Elements icon_list = element.select("div span span[class=city]");
            if (icon_list.size() > 0) {
                // 车辆城市
                String cityName = icon_list.html();
                carDto.setBrand_source_city_name(cityName);
            }
            // 原车质保
            Elements select = element.select("div[class=icon-list] a");
            if (select.size() > 0) {
                String originalCarWarranty = select.attr("title");
                carDto.setOriginalCarWarranty(originalCarWarranty);
            }
            voList.add(carDto);
        }

        return voList;
    }

    /**
     * @return response response
     * @Author chl
     * @Description 懂车帝车辆信息
     * @Date 15:16 2023/4/20
     **/
    public static List<DongCheDiCarDto> getDongCheDi(String city) {
        if (StringUtil.isEmpty(city)) {
            city = "全国"; // 默认全国
        }

        String url = "https://www.dongchedi.com/motor/pc/sh/sh_sku_list?aid=1839&app_name=auto_web_pc";
        Map map = new HashMap<>();
//		map.put("aid","1839");
//		map.put("app_name","auto_web_pc");
        map.put("brand", "3"); // 车辆品牌
        map.put("sh_city_name", city); // 城市
        map.put("series_ids", "3928"); // 车型ID
        map.put("page", "1");
        map.put("limit", "10000");
        HttpClientResult httpClientResult = null;
        try {
            httpClientResult = HttpClientUtils.doPost(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map parse = JSON.parseObject(httpClientResult.getContent(), Map.class);
        Object data = parse.get("data");
        Map respData = JSON.parseObject(JSON.toJSONString(data), Map.class);
        Object respList = respData.get("search_sh_sku_info_list");
        List<DongCheDiCarDto> dongCheDiCarDtos = JSON.parseArray(JSON.toJSONString(respList), DongCheDiCarDto.class);

        System.out.println("懂车帝:" + dongCheDiCarDtos);
        return dongCheDiCarDtos;
    }
}
